package com.mybatis.study.binding;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collection;

import com.mybatis.study.session.SqlSession;

public class MapperProxy implements InvocationHandler {

	private SqlSession sqlSession;
	
	
	
	public MapperProxy(SqlSession sqlSession) {
		super();
		this.sqlSession = sqlSession;
	}



	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO Auto-generated method stub
		//Collection是集合类的父类，方法返回参数是不是集合
		if(Collection.class.isAssignableFrom(method.getReturnType())) {
			//找到方法的类的地址名称+调用方法
			return sqlSession.selectList(method.getDeclaringClass().getName()+"."+method.getName(), args==null?null:args[0]);
		}else {
			return sqlSession.selectOne(method.getDeclaringClass().getName()+"."+method.getName(), args==null?null:args[0]);
		}
	}

}
